package com.hong.servicehosp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.hong.commonutil.exception.YyghException;
import com.hong.commonutil.result.ResultCodeEnum;
import com.hong.model.model.hosp.HospitalSet;
import com.hong.model.vo.hosp.HospitalSetQueryVo;
import com.hong.model.vo.order.SignInfoVo;
import com.hong.servicehosp.mapper.HospitalSetMapper;
import com.hong.servicehosp.service.HospitalSetService;
import com.hong.serviceutil.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;

@Service
public class ImplHospitalSetService extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {
    @Autowired
    HospitalSetMapper hospitalSetMapper;

    @Override
    public List<HospitalSet> findAllHospitalSet() {
        return hospitalSetMapper.selectAll();
    }
    @Override
    public Boolean deleteHospitalSetById(Long id) {
        return hospitalSetMapper.logicDeleteByPrimaryKey(id) == 1;
    }

    @Override
    public List<HospitalSet> findPageHospitalSet(long current, long limit, HospitalSetQueryVo hospitalSetQueryVo) {
        PageHelper.startPage((int) current, (int) limit);
        return hospitalSetMapper.findPageHospitalSet(hospitalSetQueryVo);
    }
    @Override
    public int saveHospitalSet(HospitalSet hospitalSet) {
        //设置状态为1表示可以使用，0表示不能使用
        hospitalSet.setStatus(1);
        //生成秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        return hospitalSetMapper.insert(hospitalSet);
    }

    @Override
    public HospitalSet getHospitalSetById(long id) {
        return hospitalSetMapper.selectById(id);
    }
    @Transactional
    @Override
    public int updateHospitalSetById(HospitalSet hospitalSet) {
        int res=hospitalSetMapper.updateById(hospitalSet);
        return res;
    }

    @Override
    public int batchDeleteHospitalSet(List<Long> ids) {
        return hospitalSetMapper.deleteByPrimaryKeys(ids);
    }

    @Override
    public String getSignKey(String hostCode) {
        return hospitalSetMapper.getgetSignKeyByHostCode(hostCode);
    }

    @Override
    public void checkHospSign(String hostCode, String hospSign) {
        String signKey = getSignKey(hostCode);
        String signKeyMD5 = MD5.encrypt(signKey);
        if (signKeyMD5.equals(hospSign)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
    }
    /**
     * 根据 hoscode获取医院签名信息
     * @param hoscode
     * @return
     */
    @Override
    public SignInfoVo getSignInfoVo(String hoscode) {
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        wrapper.eq("hoscode", hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(wrapper);
        if(hospitalSet == null){
            throw new YyghException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        SignInfoVo signInfoVo = new SignInfoVo();
        signInfoVo.setApiUrl(hospitalSet.getApiUrl());
        signInfoVo.setSignKey(hospitalSet.getSignKey());

        return signInfoVo;
    }
}
